var win = Titanium.UI.currentWindow;
Titanium.UI.setBackgroundColor('#000');
if (Ti.Platform.osname == 'iphone'){
	var aviso = Titanium.UI.createActivityIndicator({
   	 bottom:50, 
 	   height:55,
 	   width:190,
 	   color:'#FFFFFF',
  	  backgroundColor:'#000',
  	  opacity:0.9,
  	  borderRadius:5,
  	  borderColor:'#000',
  	  font:{fontFamily:'Helvetica Neue', fontSize:13},
  	  message:'Espere Porfavor...',	
  	 //style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
	});
}
else {
var aviso = Titanium.UI.createActivityIndicator({
   	 height:40,
   	 width:10,
   	 message:'Espere Porfavor...',	
	});
}      
aviso.show();
win.add(aviso);
var menus = Titanium.UI.createTableView({
	//style:Titanium.UI.iPhone.TableViewStyle.GROUPED
});
/////
var data = [];
 
var xhr = Ti.Network.createHTTPClient();
xhr.open("GET","http://univas.mx/appunivas/index.php?option=com_ninjarsssyndicator&feed_id=1&format=raw");
xhr.onload = function()
{
    try
    {
        var doc = this.responseXML.documentElement;
        var items = doc.getElementsByTagName("item");
        var x = 0;
        var doctitle = doc.evaluate("//channel/title/text()").item(0).nodeValue;
	 var row0 = Ti.UI.createTableViewRow({height:80,url:'http://www.conexionunivas.mx/'});
	var av0 = Ti.UI.createImageView({
						image:'../imgs/univas.png',
						height:'72',
						width:'150'
					});
	row0.add(av0);
            data[x++] = row0;
        for (var c=0;c<items.length;c++)
        {
            var item = items.item(c);
            var title = item.getElementsByTagName("title").item(0).text;
			var bgcolor = (c % 2) == 0 ? '#fff' : '#eee';

            var row = Ti.UI.createTableViewRow({height:80,backgroundColor:bgcolor,hasChild:'true'});
            var label = Ti.UI.createLabel({
                text:title,
                left:72,
                top:1,
                bottom:1,
                right:5,
				textAlign:'left',
				color:'#444444',
				font:{fontFamily:'Trebuchet MS',fontSize:14,fontWeight:'bold'}            
            });
			var av = Ti.UI.createImageView({
						image:'../imgs/noti.png',
						left:0,
						//top:'auto',
						height:48,
						width:48
					});
	        row.add(av);
            row.add(label);
            data[x++] = row;
            row.url = item.getElementsByTagName("link").item(0).text;
			row.c
        }
        var tableview = Titanium.UI.createTableView({data:data});
        Titanium.UI.currentWindow.add(tableview);
        tableview.addEventListener('click',function(e)
        {
            var w = Ti.UI.createWindow({title:doctitle});
            var wb = Ti.UI.createWebView({url:e.row.url});
            w.add(wb);
            var b = Titanium.UI.createButton({
                title:'regresar',
                //style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
            });
            w.setLeftNavButton(b);
            b.addEventListener('click',function(e)
            {
                w.close();
            });
            w.open({modal:true});
        });
    }
    catch(E)
    {
        alert(E);
    }
	aviso.hide();

};
if (Ti.Platform.osname == 'iphone'){
	var r = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.REFRESH	
	});
	r.addEventListener('click',function(e)
	{	
		// reload feed
		aviso.show();
		xhr.open("GET","http://www.univas.mx/v1/index.php?format=feed&type=rss");
		xhr.send();	
	});
	Titanium.UI.currentWindow.setRightNavButton(r);
}
xhr.send();
/*
win.addEventListener('open', function(e){
	win.add(aviso);
});*/
